package com.lanyou.esb.cook.proxy.redis.proxy.impl;

import java.io.IOException;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.Pipeline;
import redis.clients.jedis.exceptions.JedisException;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.lanyou.esb.cook.proxy.common.Constants4Redis;
import com.lanyou.esb.cook.proxy.redis.JedisPoolUtils;
import com.lanyou.esb.cook.proxy.redis.proxy.base.BaseStrJedisProxy;

/**
 * 平台流量分析
 * 
 * @author Davey.wu
 */
public class QuantityAnalysisJedisProxy extends BaseStrJedisProxy<Long> {
	/** 平台总流量前缀 **/
	public static final String KEYS_SUFFIX = Constants4Redis.PREFIX_INESBTOTALTIME;
	/** 请求响应类型（成功/失败)前缀 **/
	public static final String KEYS_SUFFIX_SF = Constants4Redis.PREFIX_REQRESTYPE;

	public QuantityAnalysisJedisProxy() {
		this.setTimeOut(Constants4Redis.QUANTITY_ANALYSIS_RETENTION_TIME);
	}

	@Override
	public String getKey(String field) {
		return KEYS_SUFFIX + KEY_SEPARATOR + field;
	}

	@Override
	public String getKeyFromFields(String... field) {
		if (field == null) {
			return KEYS_SUFFIX;
		}
		String key = KEYS_SUFFIX;
		for (String str : field) {
			key += KEY_SEPARATOR + str;
		}
		return key;
	}

	/**
	 * 获取请求成功（或失败）键值
	 * 
	 * @param field
	 *            :域
	 * @return
	 */
	public String generateSfKey(String field) {
		return KEYS_SUFFIX_SF + KEY_SEPARATOR + field;
	}

	/**
	 * 获取请求成功（或失败）键值
	 * 
	 * @param fields
	 *            ：域列表
	 * @return
	 */
	public String generateSfKey(String... fields) {
		if (fields == null) {
			return KEYS_SUFFIX_SF;
		}
		String key = KEYS_SUFFIX_SF;
		for (String str : fields) {
			key += KEY_SEPARATOR + str;
		}
		return key;
	}

	@Override
	public String getKeyFromObject(Long value) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Long get(String key) throws JedisException, JsonParseException,
			JsonMappingException, IOException {
		String result = "";
		Jedis jedis = JedisPoolUtils.getJedis();
		if (jedis == null) {
			return 0l;
		}
		boolean broken = false;
		try {
			result = jedis.get(key);
		} catch (Exception e) {
			broken = true;
		} finally {
			JedisPoolUtils.returnRes(jedis, broken);
		}
		try {
			return Long.parseLong(result);
		} catch (Exception e) {
			return 0l;
		}
	}

	/**
	 * 设置平台流量
	 * @param section：时间区域
	 * @param type：请求结果类型
	 * @param value：值
	 * @return
	 * @throws JsonProcessingException
	 */
	public long setex(String section, String type, Long value)
			throws JsonProcessingException {
		if (value == null) {
			return 0l;
		}
		long result = 0;
		Jedis jedis = JedisPoolUtils.getJedis();
		if (jedis == null) {
			return 0l;
		}
		boolean broken = false;
		try {
			Pipeline pipe = jedis.pipelined();
			// 增加平台总流量
			pipe.incrBy(this.getKey(section), value);
			// 增加请求成功（或失败）数
			pipe.incrBy(this.generateSfKey(section, type), value);
			pipe.sync();
			result = 1;
		} catch (Exception e) {
			broken = true;
		} finally {
			JedisPoolUtils.returnRes(jedis, broken);
		}
		return result;
	}
}
